import { CustomerService } from './../../../@core/services/customer/customer.service';
import { Customer } from './../../../@core/data/customer';
import { Component, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { AddCustimerDiscountRateComponent } from './add-custimer-discount-rate/add-custimer-discount-rate.component';
import { EditCustomerDiscountRateComponent } from './edit-customer-discount-rate/edit-customer-discount-rate.component';

@Component({
  selector: 'app-customer-discount-rate',
  templateUrl: './customer-discount-rate.component.html',
  styleUrls: ['./customer-discount-rate.component.scss']
})
export class CustomerDiscountRateComponent {
  tableData = []
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  config = {
    id: 'Add',
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }//弹窗配置
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'code',
        header: '操作',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '折扣率',
        fieldType: 'text'
      },
    ]
  };//表格数据
  msgs: { severity: any; summary: any; content: any; }[];
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field: 'name',
      width: '150px'
    },
    {
      field: 'name',
      width: '150px'
    },
    {
      field: 'name',
      width: '150px'
    },
  ];//表格宽度配置
  checkedIds: any = [];
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  constructor(private SharedMethodService: SharedMethodService, private CustomerService: CustomerService, private dialogService: DialogService,) { }
  ngOnInit(): void {
    this.CustomerService.GetPagedCustomerDiscount(this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.tableData = data.items
      this.pager.total = data.totalCount
    })
  }
  creat(dialogtype?: string) {
    let data = {
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data: data,
      title: '添加客户折扣率',
      content: AddCustimerDiscountRateComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if (results.modalContentInstance.form.customerDiscountName && results.modalContentInstance.form.discount) {
              this.showToast('success', '成功', '更新成功')
              this.CustomerService.CreateCustomerDiscount(results.modalContentInstance.form).subscribe((data: any) => {
                this.CustomerService.GetPagedCustomerDiscount(this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
                  this.tableData = data.items
                  this.pager.total = data.totalCount
                })
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
              }
            )
              results.modalInstance.hide();
            } else {
              data.display = true
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  edit(rowItem: any,dialogtype?: string) {
    let data = {
      data: rowItem,
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data: data,
      title: '编辑客户折扣率',
      content: EditCustomerDiscountRateComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if (results.modalContentInstance.form.customerDiscountName && results.modalContentInstance.form.discount) {
              this.CustomerService.UpdateCustomerDiscount(rowItem.customerDiscountId,results.modalContentInstance.form).subscribe((data: any) => {
                this.showToast('success', '成功', '更新成功')
                this.CustomerService.GetPagedCustomerDiscount(this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
                  this.tableData = data.items
                  this.pager.total = data.totalCount
                })
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
              }
            )
              results.modalInstance.hide();
            } else {
              data.display = true
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  delete(id: any, dialogtype?: string) {
    const results = this.dialogService.open({
      ...this.config,
      title: '删除',
      content: '确定删除这条数据？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.CustomerService.DeleteCustomerDiscount(id).subscribe((data: any) => {
              results.modalInstance.hide();
              this.showToast('success', '成功', '删除成功')
              this.CustomerService.GetPagedCustomerDiscount(this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
                this.tableData = data.items
                this.pager.total = data.totalCount
              })
            },(error) => {
              if(error.status==404){
                this.showToast('error', '失败', '服务器连接失败');
              }else{
                this.showToast('error', '失败', error.error);
              }
            }
          )

          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  CheckDeleteRole() {

  }
  pageIndexChange(event: any) {


  }
  pageSizeChange(event: any) { }
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem, this.datatable)
  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable, 'customerDiscountId').subscribe((data: any) => {
      this.checkedIds = data;
    })
  }//获取选中行
  cleared() {
    this.SharedMethodService.cleared(this.checkedIds, this.tableData, this.datatable).subscribe((data: any) => {
      this.checkedIds = data;

    })
  }//清空选中行
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
}
